package com.hxd.mall.utils.open

import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.hxd.mall.constant.HxdGlobalInfo.instance
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.content.ContextCompat.getSystemService


/**
 * 单位换算工具类
 */
object UIUtil {
    /**
     * dip转换px
     */
    fun dip2px(dip: Int): Int {
        val scale = instance.resources.displayMetrics.density
        return (dip * scale + 0.5f).toInt()
    }

    /**
     * 隐藏软键盘
     * @param context :上下文
     * @param view    :一般为EditText
     */
    fun hideKeyboard(view: View) {
        val manager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        manager.hideSoftInputFromWindow(view.windowToken, 0)
    }

    /**
     * 隐藏软键盘
     * @param context :上下文
     * @param view    :一般为EditText
     */
    fun shKeyboard(view: View) {
        var imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)

        imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}